package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

import java.awt.*;

@EnableWebSecurity

@Configuration

public class SecurityConfig {
    @Autowired

    private DatabaseUserDetailsService databaseUserDetailsService;
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        http
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/user/","/register/","/user/register").permitAll()
                        .anyRequest().authenticated()
                )
                .userDetailsService(databaseUserDetailsService)
                .formLogin(form -> form
                        .loginPage("/login")
                        .permitAll()
                );
            //.csrf().disable();
        return http.build();
    }


    @Bean

    public PasswordEncoder passwordEncoder() {

        return new BCryptPasswordEncoder(16);

    }
}
